<!--试剂警告设置阈值-->
<template>
  <div>
    <!--    查，增-->
    <el-row>
      <el-col :span="5">
        <el-input v-model="inputs" clearable placeholder="根据仪器名搜索"></el-input>
      </el-col>
      <!--select-->
      <el-col :span="2" :style="{marginLeft:'1vw'}">
        <el-button type="primary"  @click="search(inputs)">搜索</el-button>
      </el-col>
      <!--add-->
      <el-col :span="3">
        <el-button type="primary" @click="adds()">添加</el-button>
      </el-col>
    </el-row>
    <!--    表格数据-->
    <el-table :data="tableData"
              border :style="{marginTop:'2vh'}"
              :header-cell-style="{textAlign: 'center'}"
              :cell-style="{ textAlign: 'center' }">
      <el-table-column prop="inName" label="仪器" ></el-table-column>
      <el-table-column prop="itName" label="项目" ></el-table-column>
      <el-table-column prop="inValue" label="阀值" ></el-table-column>
      <el-table-column label="操作" >
        <template slot-scope="scope">
          <el-button type="primary" class="el-icon-edit" size="small" @click="edit(scope.row)">修改</el-button>
          <el-button type="danger" class="el-icon-delete" size="small" @click="del(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 修改弹窗 -->
    <el-dialog title="修改阀值" :visible.sync="dialogFormVisible">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="仪器" prop="inName">
          <el-input v-model="ruleForm.inName" readonly></el-input>
        </el-form-item>
        <el-form-item label="项目" prop="itName">
          <el-input v-model="ruleForm.itName" readonly></el-input>
        </el-form-item>
        <el-form-item label="阈值(数字)" prop="inValue">
          <el-input
            placeholder="必须是纯数字"
            oninput="value=value.replace(/[^\d]/g,'')" maxlength="3"
            v-model="ruleForm.inValue"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="sumbitEditRow('ruleForm')">确 定</el-button>
      </div>
    </el-dialog>
    <!--分页-->
    <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      :page-size="size"
      @current-change="page"
    ></el-pagination>
    <!--新增弹窗-->
    <el-dialog title="新增" :visible.sync="el_show">
      <el-form
        :model="addForm"
        :rules="rules"
        ref="addForm"
        label-width="100px"
        class="demo-addForm"
      >
        <el-form-item label="仪器" prop="inName" width="150">
          <el-select v-model="addForm.inName" placeholder="请选择">
            <el-option
              v-for="item in tableData2"
              :key="item.inName"
              :label="item.inName"
              :value="item.inName">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="项目" prop="itName">
          <el-select v-model="addForm.itName" placeholder="请选择">
            <el-option
              v-for="item in tableData2"
              :key="item.itName"
              :label="item.itName"
              :value="item.itName">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="阀值" prop="inValue" width="150">
          <el-input placeholder="必须是纯数字"
                    oninput="value=value.replace(/[^\d]/g,'')" maxlength="3"
                    v-model="addForm.inValue"
          >
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="addsForm('addForm')">添加</el-button>
          <el-button @click="resetForm('addForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "sjjgConfig",
  inject: ['reload'],
  data: function () {
    return {
      inputs: "",
      readonly: true,//input只读
      //添加弹窗默认隐藏
      el_show: false,
      // 修改接收数组
      ruleForm: {
        inId: "",
        inName: "",
        itName: "",
        inValue: ""
      },
      //添加接收数组
      addForm: {
        inId: "",
        inName: "",
        itName: "",
        inValue: "",
      },
      rules: {},
      total: null,
      size: null,
      confirmButtonText: "confirmButtonText",
      dialogFormVisible: false,
      //表格数据（分页）
      tableData: [],
      //表格数据（全部数据，不分页）
      tableData2: []
    };
  },
  methods: {
    //修改
    sumbitEditRow(formName) {
      this.dialogFormVisible = false;
      const _this = this;
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.$axios
            .put("upd", this.ruleForm)
            .then(function (resp) {
              if ((resp.data = "true")) {
                    _this.$message({
                      type: "success",
                      message: "仪器" + _this.ruleForm.inName + "的阈值修改成功！"
                    });
                this.reload()
              }
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //修改编辑
    edit(row) {
      this.dialogFormVisible = true;
      this.ruleForm.inId = row.inId;
      this.ruleForm.inName = row.inName;
      this.ruleForm.itName = row.itName;
      this.ruleForm.inValue = row.inValue;
    },
    //删除
    del(row) {
      const _this = this;
      _this.$confirm("是否确认删除仪器" + row.inName + "？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          _this.$message({
            type: "success",
            message: "删除成功!"
          });
          this.$axios.delete("deleteById/" + row.inId)
          this.reload()
        });
    },
    //分页
    page(currentPage) {
      const _this = this;
      this.$axios
        .get("queryall/" + currentPage + "/10")
        .then(function (resp) {
          //覆盖假数据
          _this.tableData = resp.data.content;
          _this.total = resp.data.totalElements;
        });
    },
    //添加
    addsForm(formName2) {
      const _this = this;
      this.$refs[formName2].validate(valid => {
        if (valid) {
          this.$axios
            .post("save", this.addForm)
            .then(function (resp) {
              if ((resp.data = "true")) {
                _this
                  .$confirm(
                    "仪器：" + _this.addForm.inName + "的信息添加成功！",
                    "提示",
                    {
                      confirmButtonText: "确定",
                      cancelButtonText: "取消",
                      type: "warning",

                    }
                  )
                  .then(() => {
                    _this.$message({
                      type: "success",
                      message: "添加成功!"
                    });
                    _this.err()
                  })
              }
              this.reload();
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //添加重置
    resetForm(formName2) {
      this.$refs[formName2].resetFields();
    },
    //根据仪器名称模糊查询
    search(inputs) {
      if (inputs!=null&&inputs!==''){
        const _this = this;
        this.$axios
          .get("inbynamequeryall/1/10"+"?name="+inputs+"&?itname="+inputs)
          .then(function (resp) {
            console.log(resp.data);
            //覆盖数据
            _this.tableData = resp.data.content;
            _this.total = resp.data.totalElements;
            _this.size = resp.data.size;
          });
      }else {
        const _this = this;
        this.$axios
          .get("queryall/1/10")
          .then(function (resp) {
            console.log(resp.data);
            //覆盖假数据
            _this.tableData = resp.data.content;
            _this.total = resp.data.totalElements;
            _this.size = resp.data.size;
          });
      }

    },

    //新增让弹框显示
    adds: function () {
      this.el_show = true;

    },
    //取消让弹框隐藏
    err: function () {
      this.el_show = false;
    },

  },
  created() {
    const _this = this;
    //分页数据
    this.$axios
      .get("queryall/1/10")
      .then(function (resp) {
        //覆盖假数据
        _this.tableData = resp.data.content;
        _this.total = resp.data.totalElements;
        _this.size = resp.data.size;
      });
    //总数据
    this.$axios
      .get("queryall/list") //jpa接口
      .then(function (resp2) {
      //覆盖假数据
      _this.tableData2 = resp2.data;
    });
  },

}
</script>

<style scoped>

</style>
